home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 43.zip / Sources C- WorkDisk V.adf / ex / gadget.c < prev    next >
Text File  |  1987-02-15  |  12KB  |  362 lines

  1.  /**************************************************************************
  2.  *     F U N   W I T H   G A D G E T S
  3.  *
  4.  ***************************************************************************/
  5.  #include <exec/types.h>
  6.  #include <exec/nodes.h>
  7.  #include <exec/lists.h>
  8.  #include <exec/ports.h>
  9.  #include <exec/devices.h>
  10.  #include <devices/keymap.h>
  11.  #include <graphics/regions.h>
  12.  #include <graphics/copper.h>
  13.  #include <graphics/gels.h>
  14.  #include <graphics/gfxbase.h>
  15.  #include <graphics/gfx.h>
  16.  #include <graphics/clip.h>
  17.  #include <graphics/view.h>
  18.  #include <graphics/rastport.h>
  19.  #include <graphics/layers.h>
  20.  #include <intuition/intuition.h>
  21.  #include <hardware/blit.h>
  22.   
  23.  /***************************************************************************
  24.          IMPORTANT CONSTANTS
  25.  ***************************************************************************/
  26.  unsigned int mask = 0;            /*  ONE BIT SET FOR EACH OPEN */
  27.   
  28.  #define  INTUITION   0x00000001
  29.  #define  GRAPHICS    0x00000002
  30.  #define  SCREEN      0x00000004
  31.  #define  WINDOW      0x00000008
  32.  #define  COLORMAP    0x00000010
  33.  #define  MATH        0x00000020
  34.  #define  MATHTRANS   0x00000040
  35.   
  36.  #define NL 0
  37.   
  38.  /***************************************************************************
  39.  *          I N T U I T I O N      G L O B A L      V A R S
  40.  ***************************************************************************/
  41.   
  42.  struct IntuitionBase *IntuitionBase;
  43.  struct GfxBase *GfxBase;
  44.  struct IntuiMessage *message;
  45.  struct RastPort *rp;
  46.  struct Window *w;
  47.   
  48.  /***************************************************************************
  49.              G A D G E T S      L I V E      H E R E
  50.  ***************************************************************************/
  51.   
  52.  /*  Image for a custom proportional gadget */
  53.   
  54.  UWORD custimage[] = {
  55.  0x0000, 0x0000, 0x0180, 0x0660, 0x1818, 0x2004, 0x4002, 0x4002,
  56.  0x4002, 0x4002, 0x2004, 0x1818, 0x0660, 0x0180, 0x0000, 0x0000
  57.  };
  58.   
  59.  struct Image cus_image = {
  60.    0, 0,                    /* LeftEdge, TopEdge */
  61.    16, 16, 1,               /* Width, Height, Depth */
  62.    &custimage[0],           /* Pointer to bit image */
  63.    1,  0,                   /* PlanePick, Planeonoff */
  64.    NULL                     /* No other images */
  65.  };
  66.   
  67.   
  68.  struct IntuiText rtxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"R",NL};
  69.  struct IntuiText gtxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"G",NL};
  70.  struct IntuiText btxt = {2,2,JAM1,-9,2,NL,(UBYTE *)"B",NL};
  71.   
  72.  struct IntuiText ntext = {2,2,JAM1, 10, -11, NL,
  73.    (UBYTE *) "Number entry", NL};
  74.   
  75.  struct IntuiText cus_text = {2,2,JAM1, 2, -11, NL,
  76.    (UBYTE *) "Custom Prop Gadget", NL};
  77.   
  78.  struct IntuiText cus1_txt = {2,2,JAM1, 161, 23, NL,
  79.    (UBYTE *) "Vert side", &cus_text };
  80.   
  81.  struct IntuiText cus2_txt = {2,2,JAM1, 20, 50, NL,
  82.    (UBYTE *) "Hor side", &cus1_txt };
  83.   
  84.  struct IntuiText bool_str = {2,2,JAM1, 4, 1, NL,
  85.    (UBYTE *) "START", NL };
  86.   
  87.  struct IntuiText bool2_str = {2,2,JAM1, 9, 1, NL,
  88.    (UBYTE *) "STOP", NL };
  89.   
  90.  struct Image    r_img, g_img, b_img;
  91.  struct PropInfo r_prop,g_prop,b_prop;
  92.   
  93.  struct PropInfo cust_prop = {
  94.     FREEHORIZ | FREEVERT,            /* Want knob to go both vert and horiz */
  95.     0x8000,  0x8000,                 /* Want knob to be centered initially  */
  96.     0x800,   0x800,                  /* Smallest increment the knob can move */
  97.     150, 50,                         /* cWidth, cHeight - Container w & h */
  98.     1, 1,                            /* HPosres, VPotres - Pot increments */
  99.     0, 0                             /* Container borders  */
  100.  };
  101.   
  102.   
  103.  #define STRINGSIZE 512
  104.  UBYTE DefString[STRINGSIZE] = "000000000000";
  105.  UBYTE Undo [STRINGSIZE];
  106.   
  107.  struct StringInfo TexString = {
  108.     DefString,                      /* Buffer - Pointer to Buffer */
  109.     Undo,                           /* UndoBuffer - Undo buf ptr  */
  110.     0,                              /* BufferPos - Init Chr Posn */
  111.     STRINGSIZE,                     /* MaxChars - Max number of Chars */
  112.     0, 0,                           /* DispPos - First Disp Chr */
  113.     13,                             /* NumChars - Number of Characters */
  114.     0, 0, 0,                        /* Posn Vars calc by Intuition */
  115.     NULL,                           /* No pointer to Rasport */
  116.     0,                              /* Longint Value */
  117.     NULL                            /* No pointer to alt Keyboard */
  118.  };
  119.   
  120.  SHORT Pairs[] = {
  121.  -1,  -1,                            /* Information describing the */
  122.  160, -1,                           /* border around the gadget */
  123.  160, 9,
  124.  -1, 9,
  125.  -1, -1
  126.   };
  127.   
  128.  SHORT Pairs1[] = {
  129.   0,  0,
  130.  51,  0,
  131.  51, 11,
  132.  0,  11,
  133.  0,  0
  134.  };
  135.   
  136.  #define NUM_PAIRS 5                /* There are Four pairs above */
  137.   
  138.  struct Border StrBorder = {
  139.   -1, -1,                           /* LeftEdge, TopEdge */
  140.   1, 0, JAM1,                       /* FrontPen,  BackPen  DrawMode  */
  141.   NUM_PAIRS,                        /* Number of XY Pairs */
  142.   Pairs,                      /* XY, Pointer to XY Pairs */
  143.   NULL                              /* No more borders */
  144.  };
  145.   
  146.  struct Border butt_border = {
  147.   -1, -1,
  148.   1, 0, JAM1,
  149.   NUM_PAIRS,
  150.   Pairs1,
  151.   NULL
  152.  };
  153.   
  154.  #define BLUE_GADGET 0
  155.  struct Gadget blue_gad = {
  156.     NL, 17,112, 150,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  157.     PROPGADGET,(APTR)&b_img, NL,
  158.     &btxt, NL,(APTR)&b_prop, BLUE_GADGET, NL
  159.  };
  160.   
  161.   
  162.  #define GREEN_GADGET 1        /* Another personal ID */
  163.  struct Gadget green_gad = {
  164.     &blue_gad, 17,97, 120,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  165.     PROPGADGET,(APTR)&g_img, NL,
  166.     >xt, NL,(APTR)&g_prop, GREEN_GADGET, NL
  167.  };
  168.   
  169.   
  170.  #define RED_GADGET 2          /* And yet another gadget */
  171.  struct Gadget red_gad = {
  172.     &green_gad, 17,82, 90,11, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  173.     PROPGADGET,(APTR)&r_img, NL,
  174.     &rtxt, NL,(APTR)&r_prop, RED_GADGET, NL
  175.  };
  176.   
  177.   
  178.  #define TEX_GAD 3
  179.  struct Gadget tex_gad = {
  180.     &red_gad, 30, 30, 150,11, GADGHCOMP, STRINGCENTER | LONGINT | RELVERIFY,
  181.     STRGADGET, (APTR)&StrBorder, NL,
  182.     &ntext, NL, (APTR)&TexString, TEX_GAD, NL
  183.  };
  184.   
  185.  #define CUST_KNOB 4
  186.  struct Gadget cust_knob = {
  187.     &tex_gad, 17, 140, 150, 50, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  188.     PROPGADGET, (APTR)&cus_image, NL,
  189.     &cus2_txt, NL, (APTR)&cust_prop, CUST_KNOB, NL
  190.  };
  191.   
  192.  #define BOOL_GAD1 5
  193.  struct Gadget bool_gad = {
  194.     &cust_knob, 30, 46, 50, 10, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  195.     BOOLGADGET, (APTR)&butt_border, NL,
  196.     &bool_str, NL, NL, BOOL_GAD1, NL
  197.  };
  198.   
  199.  #define BOOL_GAD2 6
  200.  struct Gadget bool2_gad = {
  201.    &bool_gad, 30, 60, 50, 10, GADGHCOMP, GADGIMMEDIATE | RELVERIFY,
  202.    BOOLGADGET, (APTR)&butt_border, NL,
  203.    &bool2_str,  NL, NL, BOOL_GAD2, NL
  204.  };
  205.   
  206.  /***************************************************************************
  207.  *                  N E W     W I N D O W     S T R U C T U R E
  208.  ***************************************************************************/
  209.   
  210.  struct NewWindow nw = {
  211.    0, 0,                  /*  Start position                               */
  212.    320, 200,              /*  width, height,                               */
  213.    0, 1,                  /*  detail, block pens                           */
  214.    CLOSEWINDOW            /*  IDCMP flags                             */
  215.  | REFRESHWINDOW
  216.  | MOUSEBUTTONS
  217.  | MOUSEMOVE
  218.  | GADGETDOWN
  219.  | GADGETUP,
  220.                           /*  Regular flags for gadgets and such           */
  221.    WINDOWDEPTH
  222.  | WINDOWSIZING
  223.  | WINDOWDRAG
  224.  | REPORTMOUSE
  225.  | WINDOWCLOSE
  226.  | SMART_REFRESH,
  227.   
  228.    &bool2_gad,             /* First gadget in list                          */
  229.    NULL,                  /* User checkmark                                */
  230.    "Fun with Gadgets",    /* Window Title                                  */
  231.    NULL,                  /* Pointer to screen (Set later)                 */
  232.    NULL,                  /* Pointer to superbitmap                        */
  233.    0, 0, 320, 186,        /* Ignored because not sizeable                  */
  234.    WBENCHSCREEN,          /* Using the Workbench screen                    */
  235.    };
  236.   
  237.  /***************************************************************************
  238.                   M A I N     P R O G R A M     M O D U L E
  239.  ***************************************************************************/
  240.  main()
  241.  {
  242.     ULONG MessageClass;
  243.     USHORT code;
  244.     int good_boy = FALSE;     /*  Be a "Bad boy", and hog the system  **/
  245.     void *OpenLibrary();
  246.     struct Window *OpenWindow();
  247.     struct Message *GetMsg();
  248.   
  249.  /***************************************************************************
  250.             Must Initialize the Proportional "Specialinfo" before
  251.        opening up the window.   Init Flags,  and position.
  252.  ***************************************************************************/
  253.   
  254.     r_prop.Flags = g_prop.Flags = b_prop.Flags = FREEHORIZ | AUTOKNOB;
  255.     r_prop.HorizBody = g_prop.HorizBody = b_prop.HorizBody = 0x1000;
  256.     r_prop.HorizPot = g_prop.HorizPot = b_prop.HorizPot = 0x8000;
  257.   
  258.  /***************************************************************************
  259.             Read in the ligraries,  and set each "read" mask bit.
  260.  ***************************************************************************/
  261.   
  262.     if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L)))
  263.     {
  264.        printf("no graphics library!!!\n");
  265.        close_things();
  266.        exit(1);
  267.     }
  268.     mask |= GRAPHICS;
  269.   
  270.   if(!(IntuitionBase = (struct IntuitionBase *)
  271.         OpenLibrary("intuition.library",0L)))
  272.     {
  273.        printf("no intuition here!!\n");
  274.        close_things();
  275.        exit(2);
  276.     }
  277.     mask |= INTUITION;
  278.   
  279.  /***************************************************************************
  280.                  OPEN UP THE WINDOW ON TOP OF WORKBENCH SCREEN
  281.  ***************************************************************************/
  282.   
  283.     if (!(w = (struct Window *)OpenWindow(&nw) ))
  284.     {
  285.        printf("could not open the window\n");
  286.        close_things();
  287.        exit(3);
  288.     }
  289.     mask |= WINDOW;
  290.   
  291.  /***************************************************************************
  292.                   INITIALIZATION BEFORE ENTERING MAIN LOOP
  293.  ***************************************************************************/
  294.   
  295.     rp = w->RPort;
  296.     RefreshGadgets(&red_gad,w,NULL);
  297.   
  298.  /***************************************************************************
  299.                              MAIN EVENT LOOP
  300.  ***************************************************************************/
  301.   
  302.     for (;;)
  303.     {
  304.   
  305.        if (message = (struct IntuiMessage *)GetMsg(w->UserPort))  {
  306.          MessageClass = message->Class;
  307.          code = message->Code;
  308.          ReplyMsg(message);
  309.          switch (MessageClass) {
  310.   
  311.             case GADGETUP    :
  312.             case GADGETDOWN  : do_gadgets(message, w);
  313.                                break;
  314.   
  315.             case CLOSEWINDOW : close_things();
  316.                                exit(0);
  317.                                break;
  318.             case MOUSEBUTTONS: break;
  319.          }   /* Case */
  320.        }  /* if */
  321.     }  /* for */
  322.  } /* main */
  323.  /***************************************************************************
  324.                             HANDLE THE GADGETS
  325.  ***************************************************************************/
  326.  do_gadgets (mes, win)
  327.   
  328.  struct IntuiMessage *mes;
  329.  struct Window *win;
  330.  {
  331.          struct Gadget *igad;     /* Ptr to gadget that Intuition found    */
  332.          int  gadgid;             /* ID Code identifying which gadget      */
  333.          ULONG val;
  334.   
  335.       igad = (struct Gadget *) mes->IAddress;      /* Ptr to a gadget      */
  336.          gadgid = igad->GadgetID;    /* My own personal code for this gad  */
  337.          val = (ULONG)TexString.LongInt;
  338.          switch(gadgid) {
  339.   
  340.              case GREEN_GADGET: break;
  341.              case BLUE_GADGET : break;
  342.              case TEX_GAD     : printf("got here ...\n");
  343.                                 printf("val = %ld\n", val);
  344.                                 break;
  345.          }
  346.  }
  347.   
  348.   
  349.  /***************************************************************************
  350.                            CLOSE EVERYTHING DOWN
  351.  ***************************************************************************/
  352.   
  353.  close_things()
  354.  {
  355.     if (mask & WINDOW)    CloseWindow(w);
  356.     if (mask & GRAPHICS)  CloseLibrary(GfxBase);
  357.      OpenWorkBench();
  358.     if (mask & INTUITION) CloseLibrary(IntuitionBase);
  359.  }
  360.   
  361.  
  362.